package whf.framework.workflow.assign;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import whf.framework.exception.FindException;
import whf.framework.security.dao.DeptDAO;
import whf.framework.security.dao.RoleDAO;
import whf.framework.security.dao.UserDAO;
import whf.framework.security.entity.Dept;
import whf.framework.security.entity.Role;
import whf.framework.security.entity.User;
import whf.framework.service.SpringService;
import whf.framework.util.BeanFactory;
import whf.framework.util.BeanUtils;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;

/**
 * 搜索user对象的dao
 * @author king
 *
 */
public class AssignmentService extends SpringService {
	private DeptDAO deptDAO;
	
	private RoleDAO roleDAO;
	
	private UserDAO userDAO;

	public static AssignmentService getAssignmentService() {
		return (AssignmentService)BeanFactory.getBean("workflow.jbpm.AssignmentService");
	}
	
	public void setDeptDAO(DeptDAO deptDAO) {
		this.deptDAO = deptDAO;
	}

	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public Collection<String> findDepts(String deptCodeString, String deptNameString) throws FindException {
		List<String> wheres = Utils.newArrayList();
		if(!StringUtils.isEmpty(deptCodeString)) {
			String[] deptsArray = StringUtils.split(deptCodeString, "/");
			for(int i=0; i < deptsArray.length; i++) {
				String dept = deptsArray[i];
				StringBuffer where = new StringBuffer("t");
				for(int j = 0; j < deptsArray.length - 1 - i; j++) {
					where.append(".parent");
				}
				where.append(".code");
				if(dept.indexOf("*") >= 0){
					where.append(" like '" + dept + "'");
				} else {
					where.append(" = '" + dept + "'");
				}
				wheres.add(where.toString());
			}
		}
		if(!StringUtils.isEmpty(deptNameString)) {
			String[] deptsArray = StringUtils.split(deptNameString, "/");
			for(int i=0; i < deptsArray.length; i++) {
				String dept = deptsArray[i];
				StringBuffer where = new StringBuffer("t");
				for(int j = 0; j < deptsArray.length - 1 - i; j++) {
					where.append(".parent");
				}
				where.append(".name");
				if(dept.indexOf("*") >= 0){
					where.append(" like '" + dept + "'");
				} else {
					where.append(" = '" + dept + "'");
				}
				wheres.add(where.toString());
			}
		}
		StringBuilder sql = new StringBuilder();
		for(int i=0; i < wheres.size(); i++) {
			if(i > 0) {
				sql.append(" and ");
			}
			sql.append(wheres.get(i));
		}
		List<Dept> depts = this.deptDAO.find(sql.toString());
		Collection<String> result = Utils.newHashSet();
		for(Dept dept: depts) {
			result.add(dept.getCode());
		}
		return result;
	}
	
	public Role findRoleByName(String name) throws FindException {
		assert name != null;
		List<Role> roles = this.roleDAO.find("t.name = '" + name + "'");
		if(!roles.isEmpty())
			return roles.get(0);
		else
			return null;
	}
	
	/**
	 * 节点<user username="" />
	 * @param username
	 * @return
	 * @throws FindException
	 */
	public Collection<String> findUsersByUsername(String username)  throws FindException{
		assert username != null;
		List<User> users = null;
		if(username.indexOf("*") >= 0) {
			username = StringUtils.replace(username, "*", "%");
			users = this.userDAO.find("t.username like '" + username + "' or t.name like '" + username + "'");
		} else {
			users = this.userDAO.find("t.username = '" + username + "' or t.name='" + username + "'");
		}
		Collection<String> usernames = Utils.newHashSet();
		if(users != null) {
			for(User user: users) {
				usernames.add(user.getUsername());
			}
		}
		return usernames;
	}
	
	/**
	 * @param dept
	 * @param expression
	 * @return
	 * @throws FindException
	 */
	public Collection<String> findUsersByDepts(Collection<String> deptCodes, String expression) throws FindException{
		Set<User> result = Utils.newHashSet();
		try{
			for(String deptCode: deptCodes) {
				Dept dept = this.deptDAO.findByCode(deptCode);
				if(dept == null) continue;
				Object o = BeanUtils.getProperty(dept, expression);
				if(o != null && o instanceof User){
					result.add((User)o);
				}
			}
			Collection<String> usernames = Utils.newHashSet();
			for(User user: result) {
				usernames.add(user.getUsername());
			}
			return usernames;
		} catch(Exception e){
			throw new FindException(e);
		}
	}
	
	public Collection<String> findUsersByHql(Collection<String> deptCodes, String roleName, String hql) throws FindException {
		return null;
	}
	
	public Collection<String> findUsersByRole(Collection<String> deptCodes, String roleName) throws FindException {
		return null;
	}
	
	public static void main(String[] args) throws Exception {
		AssignmentService s = new AssignmentService();
		s.findDepts("1/11/111/1112*/*AA", "IBM/亚太/华南*/oo*");
	}
}
